package com.jianzh5.service.impl;

import com.google.common.collect.Maps;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.dao.BaseDaoI;
import com.jianzh5.entity.TStatic;
import com.jianzh5.service.StaticServiceI;
import com.jianzh5.type.StaticTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jianzh5 on 2015/7/16.
 */
@Service("staticService")
public class StaticServiceImpl implements StaticServiceI{

    private final Logger logger = Logger.getLogger(StaticServiceImpl.class);

    @Autowired
    private BaseDaoI<TStatic> staticDao;

    @Override
    public DataTables dataTables(String order, String sort, DataPage dataPage) {
        String hql = "from TStatic t";
        String totalHql = "select count(*) from TStatic t";
        hql += " order by t."+sort+" "+order;
        List<TStatic> tlist = staticDao.findByPosition(hql, dataPage.getStart(), dataPage.getLength());
        long records = staticDao.count(totalHql);
        DataTables dt = new DataTables();
        dt.setDraw(dataPage.getDraw());
        dt.setRecordsTotal(records);
        dt.setRecordsFiltered(records);
        dt.setData(tlist);
        return dt;
    }

//    @Override
//    public List<TGameCategory> gameCategory() {
//        String hql = "from TGameCategory t";
//        List<TGameCategory> tlist = gameCategoryDao.find(hql);
//        return tlist;
//    }
//
    @Override
    public TStatic getStatic(int id) {
        TStatic tStatic = staticDao.get(TStatic.class,id);
        return tStatic;
    }
//
    @Override
    public void editStatic(TStatic tStatic) {
        if(tStatic.getId() == 0){  //添加
            TStatic t = new TStatic();
            t.setTypeName(StaticTypeEnum.getType(tStatic.getType()).getValue());
            t.setCreateDate(new Date());
            staticDao.save(tStatic);
        }else{
            TStatic t = staticDao.get(TStatic.class,tStatic.getId());
            BeanUtils.copyProperties(tStatic,t,new String[]{"createDate"});
            t.setTypeName(StaticTypeEnum.getType(tStatic.getType()).getValue());
        }
    }

    @Override
    public TStatic getStaticByType(int aboutType) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("type", aboutType);
        String hql = "from TStatic t where t.type = :type";
        TStatic t = staticDao.get(hql, params);
        return t;
    }

    //
//    @Override
//    public void delGame(int gid) {
//        TGame t = gameDao.get(TGame.class,gid);
//        gameDao.delete(t);
//    }
//
//    @Override
//    public PageView<TGame> gameList(PageView<TGame> page,String sort) {
//        String hql = "from TGame t order by t."+sort+" asc";
//        String totalHql = "select count(*) from TGame t";
//        int total = (int)gameDao.count(totalHql);
//        page = PageUtil.createPage(page,total);
//        List<TGame> tlist = gameDao.find(hql, page.getCpage(), page.getSize());
//        page.setRecords(tlist);
//        return page;
//    }
//
//    /**
//     * 完善首页游戏查询方法
//     * @param page
//     * @return
//     */
//    @Override
//    public PageView<PGame> pgameList(PageView<PGame> page,int cid,String gname,String sort) {
//        if(sort == null || sort.equals("")){
//            sort = "seq asc";
//        }
//        String hql = "from TGame t left join fetch t.TGameCategory tc where 1=1";
//        String totalHql = "select count(*) from TGame t where 1=1";
//        Map<String,Object> params = Maps.newHashMap();
//        if(cid != 0){
//            params.put("cid",cid);
//            hql += " and tc.id = :cid";
//            totalHql += " and t.TGameCategory.id = :cid";
//        }
//        if(gname != null && !gname.equals("")){
//            params.put("gname","%"+gname+"%");
//            hql += " and t.name like :gname";
//            totalHql += " and t.name like :gname";
//        }
//        hql += " order by t."+sort;
//        int total = (int)gameDao.count(totalHql,params);
//        page = PageUtil.createPage(page,total);
//        List<TGame> tlist = gameDao.find(hql, params,page.getCpage(), page.getSize());
//        List<PGame> plist = Lists.newArrayList();
//        for(TGame t : tlist){
//            PGame p = new PGame();
//            BeanUtils.copyProperties(t,p);
//            plist.add(p);
//        }
//        page.setRecords(plist);
//        return page;
//    }
//
//    @Override
//    public List<PGame> getphList() {
//        String hql = "from TGame t order by t.playtimes desc";
//        List<TGame> tlist = gameDao.findByPosition(hql, 0, 6);
//        logger.info(tlist.size());
//        List<PGame> plist = Lists.newArrayList();
//        for(TGame t : tlist){
//            PGame p = new PGame();
//            BeanUtils.copyProperties(t,p);
//            plist.add(p);
//        }
//        return plist;
//    }


}
